package org.graduation.ical.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import net.fortuna.ical4j.model.Calendar;
import org.graduation.feignapi.NoteClient;
import org.graduation.feignapi.ProjectClient;
import org.graduation.ical.mapper.UserAndICSKeyMapper;
import org.graduation.ical.service.ICSService;
import org.graduation.ical.tools.IcalFactory;
import org.graduation.model.common.ics.ICS;
import org.graduation.model.common.ics.UserAndICSKey;
import org.graduation.model.common.note.Note;
import org.graduation.model.common.project.Task;
import org.graduation.model.common.user.User;
import org.graduation.model.vo.CalendarVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

@Service
public class ICSServiceImpl implements ICSService {

    private final static String REDIS_KEY = "ical:";
    private final ZoneId zoneId = ZoneId.of("Asia/Shanghai");
    @Autowired
    private NoteClient noteClient;
    @Autowired
    private ProjectClient projectClient;
    @Autowired
    private UserAndICSKeyMapper userAndICSKeyMapper;

    @Override
    public Calendar generatorCalendar(User user) {
        // 1. 获取所有需要提醒的笔记 并转成ICS
        List<Note> alarmNote = noteClient.allAlarmNote(user.getUid());
        // 2. 获取所有需要有关user的task 并转成ICS
        List<Task> alarmTask = projectClient.alarmTask(user.getUid());
        List<ICS> icsList = new ArrayList<>();
        icsList.addAll(note2ICS(alarmNote));
        icsList.addAll(task2ICS(alarmTask, user.getUid()));
        icsList.sort(Comparator.comparing(ICS::getStart));
        return IcalFactory.createCalendar(icsList);
    }

    @Override
    public Calendar generatorCalendar(String auth) {
        UserAndICSKey userAndICSKey = userAndICSKeyMapper.selectOne(
                new LambdaQueryWrapper<UserAndICSKey>()
                        .eq(UserAndICSKey::getIcsKey,auth)
        );
        User user = new User();
        user.setUid(userAndICSKey.getUid());
        return generatorCalendar(user);
    }

    @Override
    public List<CalendarVO> getCalendarList(User user) {
        // 1. 获取所有需要提醒的笔记 并转成ICS
        List<Note> notes = noteClient.allAlarmNote(user.getUid());
        // 2. 获取所有需要有关user的task 并转成ICS
        List<Task> tasks = projectClient.alarmTask(user.getUid());
        List<CalendarVO> calendarVOList = new ArrayList<>();
        notes.forEach(note -> {
            CalendarVO calendarVO = CalendarVO.note2CalendarVO(note);
            calendarVOList.add(calendarVO);
        });
        tasks.forEach(task -> {
            CalendarVO calendarVO = CalendarVO.task2CalendarVO(task);
            calendarVOList.add(calendarVO);
        });
        calendarVOList.sort(Comparator.comparing(CalendarVO::getStart));
        return calendarVOList;
    }


    private List<ICS> note2ICS(List<Note> notes) {
        List<ICS> icsList = new ArrayList<>();
        notes.forEach(note -> {
            ICS ics = new ICS();
            ics.setUid(note.getUid());
            ics.setZoneId(zoneId);
            ics.setAllDay(false);
            if (note.getTitle().isBlank() || note.getTitle().isEmpty()) {
                ics.setSummary("备忘录");
            } else {
                ics.setSummary(note.getTitle());
            }
            ics.setLocation("");
            ics.setDescription(note.getContent());
            ics.setStart(note.getAlarmTime());
            ics.setEnd(note.getAlarmTime());
            icsList.add(ics);
        });
        return icsList;
    }

    private List<ICS> task2ICS(List<Task> tasks, Integer uid) {
        List<ICS> icsList = new ArrayList<>();
        tasks.forEach(task -> {
            ICS ics = new ICS();
            ics.setUid(uid);
            ics.setZoneId(zoneId);
            ics.setAllDay(false);
            ics.setSummary(task.getName());
            ics.setLocation("");
            ics.setDescription(task.getDescription());
            ics.setStart(task.getBeginTime());
            ics.setEnd(task.getEndTime());
            icsList.add(ics);
        });
        return icsList;
    }

    protected String getRedisKey(Integer uid) {
        return REDIS_KEY + uid + ":";
    }
}
